/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-08-22 11:25:55
 * @filename : static/nmm/src/views/forgetPassword.js
 * @version : 
 * @description : 
 */

define(function(require, exports, module){

    var FgPWView,
        mId = 'forgetPassword',reRender = false,menuName = 'forgetPassword',
        tpl = require('{view}forgetPassword.tpl');

    FgPWView = Backbone.View.extend({

        el: 'body',

        template: _.template(tpl),

        initialize: function () {},

        bindEvt : function() {
            var dom = $('#forgetPassword'),tthis = this;

            dom.find('.back').tap(function(){
                tthis.goBack();
            });

            dom.find('.btn-forgetpw').tap(function(){
                tthis.sendEmail();
            });
        },

        render: function (obj) {
            share.checkPermission(false);
            share.reRender(reRender,mId);
            if ($('#forgetPassword').length < 1) {
                $('body').append(this.template(lang)); 
                this.bindEvt(); 
            } else {
                $('#forgetPassword').css('display','block');
            }
        },

        sendEmail : function() {
            var obj = {};
                obj.email = $('input[name="email"]').val();
            if (this.validata(obj)) {
                this.ajaxSendEmail(obj);
            }
        },

        goBack : function() {
            window.history.go(-1);
        },
 
        validata : function(data) {
            var obj = {bool:true,type:1};
            if ($.trim(data.email) == '') {
                obj.bool = false;
            } else if (!util.isEmail(data.email)) {
                obj.bool = false;
            }
            obj.dom = $('.email');
            obj.msg = lang.forgetpw_email;
            share.inputError(obj);
            return obj.bool; 
        },
 
        ajaxSendEmail : function(data) {
            var dom = $('.btn-forgetpw');
            dom.attr('ajax',1);
            share.ajaxStartA(dom);
            var obj = {
                url : seajs.data.vars.apiUrl+'forget_password',
                type : 'POST',
                data : data,
                success : function(data) {
                    if (data.errcode == 110) {
                        var obj = {type:1};
                        obj.bool = false;
                        obj.dom = $('.email');
                        obj.msg = data.errmsg;
                        share.inputError(obj);
                    } else {
                        window.location.href = '#signin/whole/custom1'; 
                    } 
                },
                error : function(xhr, errorType, error) {
                    share.error('alert','http request failed!'); 
                },
                complete : function(xhr,status){
                    dom.attr('ajax',0);
                    share.ajaxEndA(dom);
                } 
            };
            $.ajax(share.ajaxControl(obj)); 
        }
    });

    module.exports = FgPWView;
});

